﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BattleCore.Events;
using PsyModules;

namespace Battle
{
    public class ScoreBoard
    {
        // Subspace command to start to toggle lvz
        private string BaseCommand = "*objset ";

        private int[] _LvzBaseIDs = new int[] { };
        /// <summary>
        /// Set all the base IDs for your lvz here - they are the object ids in your lvz
        /// The score should look like ->       000000001234
        /// base ids should start from left side^ to right ^
        /// so if you have a score with max 10 digits you will have 12 base id's
        /// </summary>
        public int[] LvzBaseIDs
        {
            get { return _LvzBaseIDs; }
            set { _LvzBaseIDs = value; }
        }

        /// <summary>
        /// Returns the string command to toggle lvz for old image and new image
        /// If you are using to update a decimal score,
        /// make sure to multiply it to take out decimal
        /// Example you are using currency and your value is 10.23
        /// Multiply by 100 to get 1023 from it.
        /// </summary>
        /// <param name="OldScore">Old player score</param>
        /// <param name="NewScore">New pleyer score</param>
        /// <returns></returns>
        public string UpdateScoreCommand(int OldScore, int NewScore)
        {
            // include lvz command in string
            string LvzCommand = BaseCommand;

            // Make it include 0's if the number is too small, and seperate it into an array
            char[] new_converted = (NewScore.ToString().PadLeft(_LvzBaseIDs.Length, '0')).ToCharArray();
            char[] old_converted = (OldScore.ToString().PadLeft(_LvzBaseIDs.Length, '0')).ToCharArray();

            // Go through the array and compare old to new
            // if there is a difference toggle old and new gfx
            for (int i = 0; i < _LvzBaseIDs.Length; i++)
            {
                // Take the next digit out of array
                int old_digit = (_LvzBaseIDs[i] + int.Parse(old_converted[i].ToString()));
                int new_digit = (_LvzBaseIDs[i] + int.Parse(new_converted[i].ToString()));

                //compare old with new, if there is no diff there is no need to update
                if (old_digit != new_digit)
                {
                    // + to turn on a lvz in ss and - to turn off, command must end with ","(an ss bug)
                    LvzCommand += "+" + new_digit + ",-" + old_digit + ",";
                }
            }

            return LvzCommand;
        }
        public string InitializeScore(int Score)
        {
            // include lvz command in string
            string LvzCommand = BaseCommand;

            // Make it include 0's if the number is too small, and seperate it into an array
            char[] new_converted = (Score.ToString().PadLeft(3, '0')).ToCharArray();

            int offset = _LvzBaseIDs.Length - new_converted.Length;

            for (int i = offset; i < _LvzBaseIDs.Length; i++)
            {
                int new_digit = (_LvzBaseIDs[i] + int.Parse(new_converted[i - offset].ToString()));

                LvzCommand += "+" + new_digit + ",";
            }

            return LvzCommand;
        }
        public Queue<string> AllClearDisplay()
        {
            Queue<string> q = new Queue<string>();
            // include lvz command in string
            string LvzCommand = BaseCommand + "+98,";

            for (int i = 0; i < _LvzBaseIDs.Length; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    int totalcount = LvzCommand.Length + 2 + (_LvzBaseIDs[i] + j).ToString().Length;
                    if (totalcount < 235)
                    {
                        LvzCommand += "-" + (_LvzBaseIDs[i] + j) + ",";
                    }
                    else
                    {
                        q.Enqueue(LvzCommand);
                        LvzCommand = BaseCommand + "-" + (_LvzBaseIDs[i] + j) + ",";
                    }
                }
                q.Enqueue(LvzCommand);
            }

            return q;
        }
        public string ClearDisplay(int Score)
        {
            // include lvz command in string
            string LvzCommand = BaseCommand + "-98,";

            // Make it include 0's if the number is too small, and seperate it into an array
            char[] new_converted = (Score.ToString().PadLeft(3, '0')).ToCharArray();

            int offset = _LvzBaseIDs.Length - new_converted.Length;

            for (int i = offset; i < _LvzBaseIDs.Length; i++)
            {
                int new_digit = (_LvzBaseIDs[i] + int.Parse(new_converted[i - offset].ToString()));

                LvzCommand += "-" + new_digit + ",";
            }

            return LvzCommand;
        }
    }
}
